package net.sky.oa.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * 部门表
 * SysDept. @author hond
 */
@Entity
@Table(name = "SYS_DEPT")//部门表
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class SysDept implements java.io.Serializable {

	private static final long serialVersionUID = 5673991033245515351L;
	
	// Fields

	protected Long id;
	private String name;      //部门名称
	private Integer type;      //类型：1普通部门、职能部门
	private String mobile;    //负责人手机
	private String tel;       //部门电话
	private Integer rank;        //层次
	private String function;  //部门职能
	private Date createtime;  //创建时间
	private String path;      //树层
	private String desn;      //部门描述
	private Integer status;    //状态：1使用2禁用
	private String principal;//负责人
	//private SysEmployee manager;//负责人
	private SysEmployee user;   //创建 人
//	private SysEmployee parentId;   //上级部门
	private SysDept parent;
	private Set<SysDept> children = new HashSet<SysDept>(0);
	private Set<SysSchool> schools = new HashSet<SysSchool>(0);
	
	
	
	private String oldname;        //记录原名称
	
	// Constructors

	/** default constructor */
	public SysDept() {
	}
	
	/**
	 * @param id
	 * @param name
	 */
	public SysDept(Long id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	
	public SysDept(Long id, String name,Integer status,SysDept parent) {
		this.id = id;
		this.name = name;
		this.status = status;
		this.parent = parent;
	}


	@Id
    @Column(name = "ID", unique = true, nullable = false)
	@SequenceGenerator(name="SQ_SYSDEPT_ID",sequenceName="SQ_SYSDEPT_ID",allocationSize=1)
	@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SQ_SYSDEPT_ID")
	public Long getId() {
        return id;
    }

    public void setId(Long id){
    	this.id = id;
    }

	/** minimal constructor */
	public SysDept(Long id) {
		this.id = id;
	}
 
	@Column(name = "NAME", length = 20)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "TYPE", length = 1)
	public Integer getType() {
		return this.type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	@Column(name = "MOBILE", length = 15)
	public String getMobile() {
		return this.mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	@Column(name = "TEL", length = 15)
	public String getTel() {
		return this.tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	@Column(name = "RANK", precision = 2, scale = 0)
	public Integer getRank() {
		return this.rank;
	}

	public void setRank(Integer rank) {
		this.rank = rank;
	}

	@Column(name = "FUNCTION", length = 100)
	public String getFunction() {
		return this.function;
	}

	public void setFunction(String function) {
		this.function = function;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "CREATETIME", length = 7)
	public Date getCreatetime() {
		return this.createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	@Column(name = "PATH", length = 20)
	public String getPath() {
		return this.path;
	}
	
	public void setPath(String path) {
		this.path = path;
	}
	
	@Column(name = "DESN", length = 100)
	public String getDesn() {
		return this.desn;
	}

	public void setDesn(String desn) {
		this.desn = desn;
	}

	@Column(name = "STATUS", length = 1)
	public Integer getStatus() {
		return this.status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	@Column(name = "PRINCIPAL", length = 10)
	public String getPrincipal() {
		return this.principal;
	}
	
	public void setPrincipal(String principal) {
		this.principal = principal;
	}
	
	/*@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "MANAGER_ID")
	public SysEmployee getManager() {
		return this.manager;
	}

	public void setManager(SysEmployee manager) {
		this.manager = manager;
	}*/

    /** * @return parent. */
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PARENT_ID")
    public SysDept getParent() {
        return parent;
    }

    /** * @param  Dept. */
    public void setParent(SysDept parent) {
        this.parent = parent;
    }
   
    /** * @return children. */
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")
    @OrderBy("rank asc, id desc")
    public Set<SysDept> getChildren() {
        return children;
    }

    /** * @param children Set. */
    public void setChildren(Set<SysDept> children) {
        this.children = children;
    }
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "USER_ID")
	public SysEmployee getUser() {
		return this.user;
	}

	public void setUser(SysEmployee user) {
		this.user = user;
	}

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "depts")
	public Set<SysSchool> getSchools() {
		return this.schools;
	}

	public void setSchools(Set<SysSchool> schools) {
		this.schools = schools;
	}
	
	@Transient
	public String getOldname() {
		return oldname;
	}

	public void setOldname(String oldname) {
		this.oldname = oldname;
	}
	
}